<?php
/*
  功能：用户地址信息 model
  作者：cxm
  时间：2019-10-23
*/

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class UserStepRecord extends BaseModel {
    use SoftDeletes;
    protected $dates = ['deleted_at']; //开启deleted_at
    protected $table='user_step_record';  //绑定表

    /**
     * 用户信息
     */
    public function user(){
        return $this->hasOne(User::class,'id','user_id');
    }

    /**
     * 用户的步数
     * @param $user_id
     * @return mixed
     */
    public static function getUserStep($user_id){
        return self::where('user_id',$user_id)->whereDate('date',date('Y-m-d'))->value('steps');
    }

    /**
     * 获取指定时间内的总步数
     * @param $start_time 开始时间
     * @param $end_time 结束时间
     * @return mixed
     */
    public static function statisticsStepsByTime($start_time, $end_time) {
        return self::whereBetween('date', [$start_time, $end_time])->sum('steps');
    }

    /**
     * 获取指定时间内的参与人数
     * @param $start_time 开始时间
     * @param $end_time 结束时间
     * @return mixed
     */
    public static function statisticsNumByTime($start_time, $end_time){
        return self::whereBetween('date', [$start_time, $end_time])->distinct('user_id')->count('user_id');
    }
}